#include "tlib_net.h"

int main(int argc, char **argv)
{
	int i, j;
	char sErrMsg[1024];
	int iRawSocket;
//	char *sMsg = "100990272000-01-2817:58:21helloooooooooooo";
	char *sH = "0123456789ABCDEF";
//	char *sMsg = "02 01 07 00 08 00 09 00 02 3F 6D 31 34 37 33 30 39 1F 30 1F 30 1F 32 30 30 30 2D 30 31 2D 32 39 1F 31 38 3A 34 38 3A 34 32 1F 68 65 6C 6C 6C 6C 6C 6C 6F 6F 6F 6F 6F 6F 6F 6F 6F 6F 6F 03 ";
	char *sMsg = "02 01 07 00 08 06 82 00 00 CD 56 31 30 30 39 39 1F 30 1F 32 37 1F 32 30 30 30 2D 30 31 2D 32 39 1F 32 32 3A 35 34 3A 31 33 1F 68 69 68 69 68 69 68 69 03 ";
	char sBuf[1024];
	short *p;
	
	i = 0;
	j = 0;
	while(sMsg[i] != 0)
	{
		sBuf[j] = (strchr(sH, sMsg[i]) - sH) * 16 + (strchr(sH, sMsg[i+1]) - sH);
		i = i+3;
		j ++;
	}
	sBuf[j] = 0;

	iRawSocket = TLib_Net_GetUDPSocket(sErrMsg);
	if (iRawSocket < 0)
	{
		printf("TLib_Net_GetUDPSocket Error: %s\n", sErrMsg);
		return -1;
	}
	
	for (i=0; i<1000; i++)
	{
		p = (short *)(sBuf+5);
		*p = htons(i);
		
		printf("Sending out pack %d\n", i);
//		if (TLib_Net_WriteUDPSocket(iRawSocket, "202.104.98.62", "11", "192.168.1.13", "8000", sMsg, strlen(sMsg), sErrMsg) < 0)
		if (TLib_Net_WriteUDPSocket(iRawSocket, "210.74.165.17", "4000", "202.103.190.46", "8000", sBuf, j, sErrMsg) < 0)
		{
			printf("TLib_Net_WriteUDPSocket Error: %s\n", sErrMsg);
			return -1;
		}
		sleep(1);
	}
	
	TLib_Net_CloseUDPSocket(iRawSocket);
	
	return 0;
}
